const BookList = require("../model/bookList");
const servReadTable = require("./serveReadTable");
const Book = require("../model/book");
exports.findUserBookLsit = async function(UserId){

    const result = await BookList.findAll({
        attributes:{exclude:['UserId',"BookId","createdAt","deletedAt","updatedAt","id"]},
        where:{
            UserId
        },
        include:{
            attributes:["id",'BookName',"author"],
            model:Book
        }
    })
    return result;
}
//添加收藏
exports.addBookList =async function(UserId,BookId){
    const result = await BookList.create({
        UserId,
        BookId
    })

    return result;
}
exports.UpBookList = async function(UserId,BookId){
    const  re  = await servReadTable.addReadTable(BookId,UserId);
    
    if(re){
        const result = await BookList.destroy({
            where:{
                UserId,
                BookId
            }
        })
    return result;
    }else{
        return null;
    }
   
}
exports.findBooklistOne = async function(UserId,BookId){
    const result = await BookList.findAll({
        where:{
            UserId,
            BookId
        }
    })
    return result;
}